<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__user">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.username" :placeholder="$t('user.username')" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.mobile" :placeholder="$t('user.mobile')" clearable></el-input>
        </el-form-item>

        <el-form-item prop="deptName" :label="$t('user.deptName')" class="dept-list">
        <el-popover v-model="deptListVisible" ref="deptListPopover" class='treeProp' placement="bottom-start" trigger="click">
           <el-input
            placeholder="输入关键字进行过滤"
            size="mini"
            v-model="filterText"></el-input>
           <el-scrollbar
            style="height:300px;"
          >
          <el-tree
            :data="deptList"
            :props="{ label: 'name', children: 'children' }"
            node-key="id"
            ref="deptListTree"
            :highlight-current="true"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            accordion
            @current-change="deptListTreeCurrentChangeHandle">
          </el-tree>
           </el-scrollbar>
        </el-popover>
        <el-input :value="dataForm.deptName" clearable  @clear='clear' v-popover:deptListPopover :placeholder="$t('user.deptName')"></el-input>
      </el-form-item>

        <el-form-item>
          <el-input v-model="dataForm.deptId" placeholder="部门" clearable hidden="hidden"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('sys:user:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('sys:user:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('sys:user:export')" type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="importHandle()">{{ $t('import') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('sys:user:update')" type="success" @click="pwdHandle()">重置密码</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="username" :label="$t('user.username')" sortable="custom" header-align="center" align="center"></el-table-column>
        <el-table-column prop="deptName" :label="$t('user.deptName')" header-align="center" align="center"></el-table-column>
        <el-table-column prop="realName" :label="$t('user.realName')" sortable="custom" header-align="center" align="center"></el-table-column>
        <el-table-column prop="email" :label="$t('user.email')" header-align="center" align="center"></el-table-column>
        <el-table-column prop="mobile" :label="$t('user.mobile')" sortable="custom" header-align="center" align="center"></el-table-column>
        <el-table-column prop="status" :label="$t('user.status')" sortable="custom" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" size="mini" type="danger">{{ $t('user.status0') }}</el-tag>
            <el-tag v-else size="mini" type="success">{{ $t('user.status1') }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" :label="$t('user.createDate')" sortable="custom" header-align="center" align="center" width="180"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('sys:user:update')" type="text" size="mini" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('sys:user:delete')" type="text" size="mini" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <upload v-if="uploadVisible" ref="upload" :uploadUrl=uploadUrl :downloadUrl=downloadUrl @refreshDataList="refreshList"></upload>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './user-add-or-update'
import Upload from '@/components/project/hjUpload'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      uploadUrl: '/sys/user/upload', // 上传链接
      downloadUrl: '/sys/user/downloadExcel', // 下载链接
      mixinViewModuleOptions: {
        getDataListURL: '/sys/user/page',
        getDataListIsPage: true,
        deleteURL: '/sys/user',
        deleteIsBatch: true,
        pwdURL: '/sys/user/passwordReset',
        exportURL: '/sys/user/export'
      },
      dataForm: {
        username: '',
        deptId: '',
        mobile: ''
      },
      uploadVisible: false,
      deptListVisible: false,
      deptList: [],
      filterText: ''
    }
  },
  components: {
    AddOrUpdate,
    Upload
  },
  created () {
    this.dataForm.deptId = this.$route.params.deptId
  },
  watch: {
    filterText (val) {
      // // console.log(val)
      // // console.log(this.$refs.menuListTree)
      this.$refs.deptListTree.filter(val)
    }
  },
  activated () {
    this.dataForm.deptId = this.$route.params.deptId || ''
    this.getDataList()
    this.getDeptList()
  },

  methods: {
    clear () {
      // console.log(1)
      this.deptListVisible = true
      this.dataForm.deptName = ''
    },
    filterNode (value, data) {
      // console.log(value, data)
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    // 密码重置
    pwdHandle (id) {
      if (!id && this.dataListSelections.length <= 0) {
        return this.$message({
          message: '请选择操作项',
          type: 'warning',
          duration: 500
        })
      }
      this.$confirm(this.$t('prompt.info', { 'handle': '重置密码' }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        // 具体代码需微调格式去除空格
        this.$http.put(this.mixinViewModuleOptions.pwdURL, id ? [id] : this.dataListSelections.map(item => item.id)).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.getDataList()
            }
          })
        }).catch(() => {})
      }).catch(() => {})
    },
    // 导入
    importHandle () {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.upload.init()
      })
    },
    // 获取部门列表
    getDeptList () {
      return this.$http.get('/sys/dept/list').then(({ data: res }) => {
        if (res.code !== 0) {
          return false
        }
        this.deptList = res.data
      }).catch(() => {})
    },
    // 所属部门树, 选中
    deptListTreeCurrentChangeHandle (data, node) {
      this.dataForm.deptId = data.id
      this.dataForm.deptName = data.name
      this.deptListVisible = false
    }
  }
}
</script>
